package hos.okhttp.exception;

import java.util.HashMap;
import java.util.Map;

import hos.http.HttpErrorType;
import hos.http.HttpException;
import hos.http.HttpParam;
import hos.http.HttpTag;
import okhttp3.Request;

/**
 * <p>Title: OkHttpException </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-25 20:11
 */
public class OkHttpException extends HttpException {

    public static String getUrl(Request request) {
        HttpTag.UrlAndBody urlAndBody = request.tag(HttpTag.UrlAndBody.class);
        if (urlAndBody != null) {
            return urlAndBody.url;
        }
        return request.url().toString();
    }

    public static Map<String, Object> getBody(Request request) {
        HttpTag.UrlAndBody urlAndBody = request.tag(HttpTag.UrlAndBody.class);
        if (urlAndBody != null) {
            if (urlAndBody.body == null) {
                return new HashMap<>();
            }
            Map<String, Object> map = new HashMap<>();
            for (HttpParam httpParam : urlAndBody.body) {
                map.put(httpParam.getKey(), httpParam.getValue());
            }
            return map;
        }
        return new HashMap<>();
    }

    public OkHttpException(HttpErrorType type, Request request,
                           Throwable cause, Object tag) {
        super(type,
                getUrl(request),
                getBody(request),
                cause,
                tag);
    }

    public OkHttpException(HttpErrorType type, Request request, Throwable cause) {
        this(type, request, cause, null);
    }

    public OkHttpException(HttpErrorType type, Request request) {
        this(type, request, null);
    }

    public static OkHttpException throwTimeout(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.timeout, request, cause, tag);
    }

    public static OkHttpException throwTimeout(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.timeout, request, cause);
    }

    public static OkHttpException throwTimeout(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.timeout, request);
    }

    public static OkHttpException throwCertificate(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.certificate, request, cause, tag);
    }

    public static OkHttpException throwCertificate(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.certificate, request, cause);
    }

    public static OkHttpException throwCertificate(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.certificate, request);
    }

    public static OkHttpException throwResponse(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.response, request, cause, tag);
    }

    public static OkHttpException throwResponse(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.response, request, cause);
    }

    public static OkHttpException throwResponse(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.response, request);
    }


    public static OkHttpException throwCancel(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.cancel, request, cause, tag);
    }

    public static OkHttpException throwCancel(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.cancel, request, cause);
    }

    public static OkHttpException throwCancel(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.cancel, request);
    }


    public static OkHttpException throwConnection(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.connection, request, cause, tag);
    }

    public static OkHttpException throwConnection(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.connection, request, cause);
    }

    public static OkHttpException throwConnection(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.connection, request);
    }


    public static OkHttpException throwUnknown(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.unknown, request, cause, tag);
    }

    public static OkHttpException throwUnknown(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.unknown, request, cause);
    }

    public static OkHttpException throwUnknown(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.unknown, request);
    }


    public static OkHttpException throwConverter(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.converter, request, cause, tag);
    }

    public static OkHttpException throwConverter(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.converter, request, cause);
    }

    public static OkHttpException throwConverter(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.converter, request);
    }

    public static OkHttpException throwDownload(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.download, request, cause, tag);
    }

    public static OkHttpException throwDownload(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.download, request, cause);
    }

    public static OkHttpException throwDownload(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.download, request);
    }


    public static OkHttpException throwUpload(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.upload, request, cause, tag);
    }

    public static OkHttpException throwUpload(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.upload, request, cause);
    }

    public static OkHttpException throwUpload(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.upload, request);
    }

    public static OkHttpException throwFailure(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.failure, request, cause, tag);
    }

    public static OkHttpException throwFailure(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.failure, request, cause);
    }

    public static OkHttpException throwFailure(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.failure, request);
    }

    public static OkHttpException throwServer(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.server, request, cause, tag);
    }

    public static OkHttpException throwServer(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.server, request, cause);
    }

    public static OkHttpException throwServer(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.server, request);
    }

    public static OkHttpException throwNetwork(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.network, request, cause, tag);
    }

    public static OkHttpException throwNetwork(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.network, request, cause);
    }

    public static OkHttpException throwNetwork(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.network, request);
    }

    public static OkHttpException throwParam(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.param, request, cause, tag);
    }

    public static OkHttpException throwParam(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.param, request, cause);
    }

    public static OkHttpException throwParam(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.param, request);
    }

    public static OkHttpException throwUrlParse(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.urlParse, request, cause, tag);
    }

    public static OkHttpException throwUrlParse(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.urlParse, request, cause);
    }

    public static OkHttpException throwUrlParse(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.urlParse, request);
    }

    public static OkHttpException throwResult(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.result, request, cause, tag);
    }

    public static OkHttpException throwResult(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.result, request, cause);
    }

    public static OkHttpException throwResult(
            Request request,
            Object tag
    ) {
        return new OkHttpException(HttpErrorType.result, request, null, tag);
    }

    public static OkHttpException throwResult(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.result, request);
    }

    public static OkHttpException throwResultNull(
            Request request, Throwable cause, Object tag
    ) {
        return new OkHttpException(HttpErrorType.resultNull, request, cause, tag);
    }

    public static OkHttpException throwResultNull(
            Request request,
            Throwable cause
    ) {
        return new OkHttpException(HttpErrorType.resultNull, request, cause);
    }

    public static OkHttpException throwResultNull(
            Request request
    ) {
        return new OkHttpException(HttpErrorType.resultNull, request);
    }

}
